package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.pojo.dto.QueryPageBeanDTO;
import com.itheima.health.pojo.entity.CheckGroup;
import com.itheima.health.pojo.entity.CheckItem;
import com.itheima.health.pojo.entity.Setmeal;
import com.itheima.health.pojo.result.PageResult;
import com.itheima.health.pojo.result.Result;
import com.itheima.health.service.SetMealService;
import com.itheima.health.utils.AliOssProperties;
import com.itheima.health.utils.AliyunSmsTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangmeng
 * @description 套餐SEVICE实现类
 * @date 2019/9/26
 **/
@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private AliOssProperties aliOssProperties;
    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("[套餐-添加]data:{},checkgroupIds:{}",setmeal,checkgroupIds);
        // 调用DAO数据入库
        // 插入基本数据
        setMealDao.insert(setmeal);
        // 插入关联数据
        for (Integer checkgroupId : checkgroupIds) {
            setMealDao.insertSetMealAndCheckGroup(setmeal.getId(),checkgroupId);
        }

    }

    @Override
    public PageResult findPage(QueryPageBeanDTO queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //DAO查询数据
        Page<Setmeal> page = setMealDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Setmeal> getSetmeal() {
        List<Setmeal> setmeals = setMealDao.selectAll();
        setmeals.forEach(setmeal -> {
            // 构造图片完整路径
            setmeal.setImg(aliOssProperties.getUrlPrefix()+setmeal.getImg());
        });
        return setmeals;
    }

    @Override
    public Setmeal findById(Integer id) {
        //调用DAO查询数据
        Setmeal setmeal = setMealDao.selectById(id);
        // 构造图片完整路径
        setmeal.setImg(aliOssProperties.getUrlPrefix()+setmeal.getImg());
        return setmeal;
    }

    @Transactional
    @Override
    public void edit(Setmeal setmeal, List<Integer> checkGroupIds) {
        //1.修改基本信息(图片链接需要处理)
        setmeal.setImg(setmeal.getImg().replace(aliOssProperties.getUrlPrefix(), ""));
        setMealDao.edit(setmeal);
        //2.删除原有的绑定信息，插入新的绑定
        Integer setmealId = setmeal.getId();
        checkGroupDao.deleteSetmealAndCheckGroupIdBySetmealId(setmealId);
        //不为空就插入
        if (checkGroupIds !=null){
            checkGroupIds.forEach(checkGroupid ->{
                 setMealDao.insertSetMealAndCheckGroup(setmealId,checkGroupid);
            });
        }
    }

    @Transactional
    @Override
    public void deleteSetmealById(Integer id) {
        //删除基本信息
        setMealDao.deleteSetmealById(id);
        //删除绑定检查组信息
        checkGroupDao.deleteSetmealAndCheckGroupIdBySetmealId(id);
    }



    /**
     * 用户端--根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public Setmeal userfindById(Integer id) {
        //查询套餐基本信息
        Setmeal setmeal = setMealDao.userfindById(id);

        List<CheckGroup> checkGroups = new ArrayList<>();
        //循环查询
        List<Integer> grouplist =  setMealDao.userfindGroupById(id);
        if (grouplist != null && grouplist.size() > 0){
            for (Integer i : grouplist) {
                CheckGroup checkGroup = checkGroupDao.userfindById(i);
                List<Integer> itemlist =  checkGroupDao.userfindGroupById(checkGroup.getId());
                if (itemlist != null && itemlist.size() > 0){
                    List<CheckItem> checkItems = new ArrayList<>();
                    for (Integer j : itemlist) {
                        CheckItem checkItem = checkItemDao.userfindById(j);
                        checkItems.add(checkItem);
                    }
                    checkGroup.setCheckItems(checkItems);
                }
                checkGroups.add(checkGroup);
            }
        }
        setmeal.setCheckGroups(checkGroups);
       return setmeal;
    }
}
